package com.xiaoq.sites.service.impl;

import com.xiaoq.apis.interpreter.*;
import com.xiaoq.apis.order.InterCalendar4UpdateVo;
import com.xiaoq.sites.entity.InterpreterCalendarEntity;
import com.xiaoq.sites.repository.IInterpreterCalendarRepository;
import com.xiaoq.sites.service.IInterpreterService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;
import com.xiaoq.sites.entity.InterpreterEntity;
import com.xiaoq.sites.repository.IInterpreterRepository;

import java.util.ArrayList;
import java.util.List;

@Service(interfaceClass=IInterpreterApi.class, version="1.0.0")
public class InterpreterApiImpl implements IInterpreterApi {

	@Autowired
	private IInterpreterRepository repo;

	@Autowired
	private IInterpreterCalendarRepository calendarRepository;

	@Autowired
	private IInterpreterService interpreterService;

	@Override
	public InterpreterInfo4SocialVo getInterpreterInfo4Social(String id) {
		InterpreterEntity entity = repo.findById(id);
		if (entity == null) {
			return null;
		}
		InterpreterInfo4SocialVo vo = new InterpreterInfo4SocialVo();
		BeanUtils.copyProperties(entity, vo);
		return vo;
	}

	@Override
	public List<InterCalendar4OrderVo> getInterCalendarList(List<String> orderIdList) {
		if(CollectionUtils.isEmpty(orderIdList)){
			return new ArrayList<>();
		}
		List<InterpreterCalendarEntity> calendarEntityList = calendarRepository.getInterCalendarListByOrderIds(orderIdList);
		if(CollectionUtils.isEmpty(calendarEntityList)){
			return new ArrayList<>();
		}

		List<InterCalendar4OrderVo> interCalendar4OrderVos = new ArrayList<>();
		String interId = calendarEntityList.get(0).getInterpreterId();
		InterpreterEntity interpreterEntity = repo.findById(interId);
		String interMobile = "";
		String interName = "";
		if(interpreterEntity != null){
			interName = interpreterEntity.getName();
			interMobile = interpreterEntity.getMobile();
		}

		for(InterpreterCalendarEntity calendarEntity : calendarEntityList){
			InterCalendar4OrderVo interCalendar4OrderVo = new InterCalendar4OrderVo();
			BeanUtils.copyProperties(calendarEntity, interCalendar4OrderVo);
			interCalendar4OrderVo.setInterName(interName);
			interCalendar4OrderVo.setInterMobile(interMobile);
			interCalendar4OrderVos.add(interCalendar4OrderVo);
		}

		return interCalendar4OrderVos;
	}

	@Override
	public InterCalendar4OrderVo getInterCalendar(String orderId) {
		if(StringUtils.isBlank(orderId)){
			return null;
		}
		InterpreterCalendarEntity interpreterCalendar = calendarRepository.getInterCalendarByOrderId(orderId);
		if(interpreterCalendar == null){
			return null;
		}
		String interId = interpreterCalendar.getInterpreterId();
		InterpreterEntity interpreterEntity = repo.findById(interId);
		if(interpreterEntity == null){
			return null;
		}
		InterCalendar4OrderVo interCalendar4OrderVo = new InterCalendar4OrderVo();
		BeanUtils.copyProperties(interpreterCalendar, interCalendar4OrderVo);
		interCalendar4OrderVo.setInterName(interpreterEntity.getName());
		interCalendar4OrderVo.setInterMobile(interpreterEntity.getMobile());

		return interCalendar4OrderVo;
	}

	@Override
	public long getInterWaitConfirmCount(String userId) {
		return calendarRepository.getInterWaitConfirmCountByUserId(userId);
	}

	@Override
	public boolean updateInterCalenarByOrderId(InterCalendar4UpdateVo interCalendar4UpdateVo) {
		if(interCalendar4UpdateVo==null || StringUtils.isBlank(interCalendar4UpdateVo.getOrderId())){
			return false;
		}
		InterpreterCalendarEntity calendarEntity = new InterpreterCalendarEntity();
		calendarEntity.setOrderId(interCalendar4UpdateVo.getOrderId());
		calendarEntity.setStatus(calendarEntity.getStatus());
		calendarEntity.setUpdatedAt(System.currentTimeMillis());
		return calendarRepository.updateInterCalendarByOrderId(calendarEntity);
	}

	@Override
	public Interpreter4OrderVo getInterByInterId(String interId) {
		if(StringUtils.isBlank(interId)){
			return null;
		}
		InterpreterEntity interpreterEntity = interpreterService.findInterpreterById(interId);
		if(interpreterEntity != null){
			Interpreter4OrderVo interpreter4OrderVo = new Interpreter4OrderVo();
			BeanUtils.copyProperties(interpreterEntity, interpreter4OrderVo);
			return interpreter4OrderVo;
		}
		return null;
	}

	@Override
	public boolean updateInter2CanAppoint(String interId, String shopId) {
		return interpreterService.updateInter2CanAppoint(interId, shopId);
	}

	@Override
	public boolean updateInter2Serving(String interId, String shopId) {
		return interpreterService.updateInter2Serving(interId, shopId);
	}

	@Override
	public boolean updateInter2WaitService(String interId, String shopId) {
		return interpreterService.updateInter2WaitService(interId, shopId);
	}

	@Override
	public Interpreter4OrderVo getInterByUserId(String userId) {
		if(StringUtils.isBlank(userId)){
			return null;
		}
		InterpreterEntity interpreterEntity = interpreterService.getInterpreterByUserId(userId);
		if(interpreterEntity != null){
			Interpreter4OrderVo interpreter4OrderVo = new Interpreter4OrderVo();
			BeanUtils.copyProperties(interpreterEntity, interpreter4OrderVo);
			return interpreter4OrderVo;
		}
		return null;
	}

	@Override
	public boolean updateInterCalendarStatus(String orderId, Integer status) {
		if(StringUtils.isBlank(orderId) || status==null){
			return false;
		}
		InterpreterCalendarEntity interpreterCalendar = new InterpreterCalendarEntity();
		interpreterCalendar.setOrderId(orderId);
		interpreterCalendar.setStatus(status);
		return interpreterService.updateInterCalendarByOrderId(interpreterCalendar);
	}

	@Override
	public Inter4UserVo getInterByMobile(String mobile) {
		if(StringUtils.isBlank(mobile)){
			return null;
		}
		InterpreterEntity interpreterEntity = interpreterService.findByMobile(mobile);
		if(interpreterEntity == null){
			return null;
		}
		Inter4UserVo inter4UserVo = new Inter4UserVo();
		BeanUtils.copyProperties(interpreterEntity, inter4UserVo);
		return inter4UserVo;
	}

	@Override
	public boolean updateInterUserIdByInterId(Inter4UserVo inter4UserVo) {
		if(inter4UserVo==null || StringUtils.isAnyBlank(inter4UserVo.getUserId(), inter4UserVo.getId())){
			return false;
		}

		InterpreterEntity inter4Update = new InterpreterEntity();
		inter4Update.setId(inter4UserVo.getId());
		inter4Update.setUserId(inter4UserVo.getUserId());

		return interpreterService.updateByInterId(inter4Update);
	}
}
